package modes.singletons;

/**
 * @author longzj
 */
public class LazyWithoutSync {

    public static void main(String[] args) {
//        simple();
        simpleWithMultiThread();
    }

    private static void simple() {
        Singleton.getInstance().print();
    }

    private static void simpleWithMultiThread() {
        final int count = 10;
        for (int i = 0; i < count; i++) {
            new TestThread(String.valueOf(i)).start();
        }
    }

    /**
     * 懒汉模式，无线程同步
     */
    private static class Singleton extends AbstractSingleton {
        private static Singleton mInstance;

        private Singleton() {
            System.out.println("simpleSingleton Constructor");
        }

        static Singleton getInstance() {
            if (mInstance == null) {
                mInstance = new Singleton();
            }
            return mInstance;
        }
    }

    private static class TestThread extends Thread {

        TestThread(String name) {
            super("TestThread " + name);
        }

        @Override
        public void run() {
            Singleton.getInstance().print();
        }
    }
}
